package com.zty.xiaomi.server.Controller;

import com.zty.xiaomi.server.Vo.CodeResult;
import com.zty.xiaomi.server.utils.SMSUtils;
import com.zty.xiaomi.server.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@RestController
@RequestMapping("/validateCode")
@CrossOrigin
public class ValidateCodeController {
    @Autowired
    private JedisPool jedisPool;

    //用户快速登陆发送验证码
    @RequestMapping("/send4Login")
    public CodeResult send4Login(String phone){
        //生成验证码
        Integer validateCode = ValidateCodeUtils.generateValidateCode(6);
        //给用户发送验证码
        try {
            SMSUtils.sendSMS(SMSUtils.VALIDATE_CODE,phone,validateCode.toString());
        } catch (Exception e) {
            e.printStackTrace();
            return new CodeResult(500, "短信发送失败");
        }
        //将验证码存入Redis
        Jedis jedis = jedisPool.getResource();
        jedis.setex(phone + ":login:code",300,validateCode.toString());
        return new CodeResult(200,"短信发送成功");
    }
}
